package org.xjtu.weteam.web.controller;


import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xjtu.weteam.common.JsonResponse;
import org.xjtu.weteam.common.utils.TokenUtils;
import org.xjtu.weteam.model.domain.Project;
import org.xjtu.weteam.model.dto.ProjectDTO;
import org.xjtu.weteam.model.dto.ProjectDateDTO;
import org.xjtu.weteam.service.ProjectService;

import java.util.List;

/**
 * @author Kevin
 */
@RestController
@RequestMapping("/api/project")
public class ProjectController {

    private final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private ProjectService projectService;

    @GetMapping("/listProjects")
    @ResponseBody
    public JsonResponse listProjects(Integer teamId, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        return JsonResponse.success(projectService.listProjects(teamId));
    }
    @GetMapping("/projectSearchById")
    @ResponseBody
    public JsonResponse projectSearchById(Integer projectId, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        return JsonResponse.success(projectService.projectSearchById(projectId));
    }

    @PostMapping("/getProjectAndItsNum")
    @ResponseBody
    public JsonResponse getProjectAndItsNum(Integer teamId, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        List<ProjectDTO> projectDTOS = projectService.selectProjectAndItsNum(teamId);
        return JsonResponse.success(projectDTOS);
    }
    @PostMapping("/insert")
    @ResponseBody
    public JsonResponse insert(ProjectDateDTO projectDateDTO, @RequestHeader("X-Access-Token") String token) {
        Project project = new Project(projectDateDTO);
        project.setUserId(TokenUtils.getIdFromToken(token));
        return JsonResponse.success(projectService.insertProject(project));
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public JsonResponse changeStatus(Integer projectId, String newStatus, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        if(projectService.changeStatus(projectId, newStatus) == 1)
            return JsonResponse.success(true, "状态已转换");
        else return JsonResponse.failure("找不到项目");
    }
}
